<template>
  <view class="wrap">
    <view class="header">
      <picker class="location" mode="region"><text class="iconfont icon-address"></text>上海市<text class="iconfont icon-xingzhuang"></text></picker>
      <view class="search">
        <text class="iconfont icon-sousuo"></text>
        <input placeholder="搜索商品名称" type="text" confirm-type="search" v-model="searchText" @confirm="onSearch"/>
        <text class="iconfont icon-biaoqianhuashucha1"></text>
      </view>
    </view>
    <view class="banner">
      <swiper style="height: 480rpx;">
        <swiper-item v-for="(item, index) in banners" :key="index">
          <image src="https://sunchang.oss-cn-beijing.aliyuncs.com/weixin/img/rent-banner.jpg" />
        </swiper-item>
      </swiper>
    </view>
    <view class="menus">
      <view class="item" v-for="(item, index) in menuList" :key="index" @click="toShopList(item.id)">
        <image :src="item.logo" mode="aspectFill"></image>
        <text>{{item.title}}</text>
      </view>
    </view>
    <view class="banners">
      <swiper style="height: 320rpx;">
        <swiper-item v-for="(item, index) in banners" :key="index">
          <image src="https://sunchang.oss-cn-beijing.aliyuncs.com/weixin/img/rent-banner.jpg" />
        </swiper-item>
      </swiper>
    </view>
    <view class="popular-shops">
      <view class="item" v-for="(item, index) in shopList" :key="index" @click="toShopDetail(item)">
        <image :src="item.logo" mode="aspectFill"></image>
        <view class="name">{{item.shopName}}</view>
        <view class="points">积分:<text>{{item.points}}</text></view>
      </view>
    </view>
    <tab-menu :current="0" />
  </view>
</template>

<script>
  import {
    Shop,
    User
  } from '@/apis'
  import {utils} from '@/utils'
  import TabMenu from '@/components/tab-menu.vue'
  
  export default {
    components: {
      TabMenu
    },
    data() {
      return {
        currentShopTypeId: 0,
        title: 'Hello',
        banners: [1, 2, 3],
        menuList: [],
        shopList: [],
        cities: [],
        searchText: ''
      }
    },
    onShow() {
      this.currentShopTypeId = 0
    },
    onLoad() {
      this.getShopTypes()
      getList.call(this)
      getCities.call(this)
      uni.getLocation({
        success: res => {
          console.log('获取当前地理位置: ', res)
          uni.setStorageSync('location', {
            latitude: res.latitude,
            longitude: res.longitude
          })
        },
        fail: err => {
          utils.wxToast()
        }
      })
    },
    methods: {
      getShopTypes() {
        Shop.getShopTypes({
          data: {
            query: {},
            form: {},
            pagination: {}
          }
        }).then(res => {
          console.log('获取店铺类型')
          console.log(res)
          this.menuList = res.data.body
        })
      },
      toShopList(id) {
        uni.redirectTo({
          url: `/pages/shopList/shopList?typeId=${id}`
        })
      },
      toShopDetail (item) {
        utils.routerTo('/pages/shopDetail/shopDetail?id=' + item.id)
      },
      getTopShops() {

      },
      onSearch () {
        uni.navigateTo({
          url: `/pages/searchList/searchList?searchText=${this.searchText}`
        })
      }
    }
  }
  function getList () {
    let userInfo = uni.getStorageSync('userInfo')
    Shop.getShopList({
      data: {
        query: {
          userId: userInfo.id,
          status: 2
        },
        pagination: {
          current: 1,
          pageSize: 6
        }
      }
    }).then(res => {
      this.isLoading = false
      if (res.data.code === 200) {
        // utils.wxToastSuccess()
        this.shopList = res.data.body
      } else {
        utils.wxToastFail()
      }
    }, err => {
      uni.stopPullDownRefresh()
      console.error('获取店铺列表失败')
      console.error(err)
    })
  }
  /**
   * 获取全国城市列表
   */
  function getCities () {
     return User.getCities().then(res => {
       console.log('获取城市列表')
       console.log(res)
     })
  }
</script>

<style lang="scss">
  .header {
    background-color: #FF4A4A;
    box-sizing: border-box;
    padding: 20rpx;

    .location {
      font-size: 32rpx;
      color: #FFFFFF;
      line-height: 1.5;
      display: flex;
      align-items: center;
      height: 100rpx;

      .icon-xingzhuang {
        font-size: 22rpx;
        margin-left: 20rpx;
      }
    }

    .search {
      height: 88rpx;
      display: flex;
      align-items: center;
      border-radius: 10rpx;
      background-color: #FFFFFF;
      box-shadow: 0 4rpx 4rpx 0 rgba(0, 0, 0, 0.24);
      box-sizing: border-box;
      padding: 0 15rpx;

      input {
        font-size: 28rpx;
        flex: 1;
      }
    }
  }

  .banner {
    image {
      width: 750rpx;
      height: 480rpx;
    }
  }

  .banners {
    border-top: #EFEFEF 20rpx solid;
    border-bottom: #EFEFEF 20rpx solid;
    background-color: #fff;
    padding: 20rpx 0;
    image {
      width: 750rpx;
      height: 320rpx;
    }
  }

  .menus {
    box-sizing: border-box;
    padding: 30rpx 20rpx;
    display: flex;
    flex-shrink: 0;
    flex-wrap: wrap;

    .item {
      width: 20%;
      padding-bottom: 30rpx;
      display: flex;
      flex-direction: column;
      align-items: center;

      image {
        width: 60rpx;
        height: 60rpx;
        background-color: #666;
        border-radius: 50%;
      }

      text {
        padding-top: 20rpx;
        font-size: 26rpx;
      }
    }
  }
  .popular-shops {
    background-color: #FFF;
    box-sizing: border-box;
    display: flex;
    flex-wrap: wrap;
    padding: 30rpx 26rpx;
    .item {
      width: 186rpx;
      margin-right: 70rpx;
      padding-bottom: 30rpx;
      &:nth-child(3n) {
        margin-right: unset;
      }
      image {
        width: 186rpx;
        height: 186rpx;
        border-radius: 10rpx;
        background-color: #F7F7F7;
      }
      .name {
        color: #333333;
        font-size: 26rpx;
        text-align: center;
        height: 60rpx;
        lighting-color: 60rpx;
      }
      .points {
        text-align: center;
        font-size: 24rpx;
        color: #666666;
        text {
          color: #FF3333;
        }
      }
    }
  }
</style>
